
package com.hexinfo.dmpro.script.publish.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hexinfo.dmpro.common.annotation.AnonymousAccess;
import com.hexinfo.dmpro.script.publish.entity.ScriptPublishVersionEntity;
import com.hexinfo.dmpro.script.publish.service.IScriptPublishVersionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;


/**
 * 脚本发布版本
 *
 * @author Wang zhihao
 * @date 2023-09-13 18:16:07
 */
@RestController
@AllArgsConstructor
@RequestMapping("/api/scriptPublish/version" )
@Api(value = "scriptPublishVersion", tags = "脚本发布版本管理")
public class ScriptPublishVersionController {

    private final IScriptPublishVersionService scriptPublishVersionService;

    /**
     * 分页查询
     * @param page 分页对象
     * @param scriptPublishVersion 脚本发布任务
     * WrapperEnhancer.like(scriptPublishVersion); //自动拼接存在值sql的方法
     * @return
     */
    @ApiOperation(value = "分页查询", notes = "分页查询")
    @GetMapping("/page" )
    public ResponseEntity<Object> getscriptPublishVersionPage(Page page, ScriptPublishVersionEntity scriptPublishVersion) {
        return new ResponseEntity<>(scriptPublishVersionService.queryList(page,scriptPublishVersion), HttpStatus.OK);
    }


    /**
     * 通过id查询脚本发布任务
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id查询", notes = "通过id查询")
    @GetMapping("/{id}" )
    public ResponseEntity<Object> getById(@PathVariable("id" ) String id) {
        return ResponseEntity.ok(scriptPublishVersionService.getById(id));
    }

    /**
     * 新增脚本发布任务
     * @param scriptPublishVersion 脚本发布任务
     * @return R
     */
    @ApiOperation(value = "新增脚本发布任务", notes = "新增脚本发布任务")
    @PostMapping
    public ResponseEntity<Object> save(@RequestBody ScriptPublishVersionEntity scriptPublishVersion) {
        return ResponseEntity.ok(scriptPublishVersionService.save(scriptPublishVersion));
    }

    /**
     * 修改脚本发布任务
     * @param scriptPublishVersion 脚本发布任务
     * @return R
     */
    @ApiOperation(value = "修改脚本发布任务", notes = "修改脚本发布任务")
    @PutMapping
    public ResponseEntity<Object> updateById(@RequestBody ScriptPublishVersionEntity scriptPublishVersion) {
        return ResponseEntity.ok(scriptPublishVersionService.updateById(scriptPublishVersion));
    }

    /**
     * 通过id删除脚本发布任务
     * @param ids
     * @return R
     */
    @ApiOperation(value = "通过id删除脚本发布任务", notes = "通过id删除脚本发布任务")
    @DeleteMapping(value = {"/{ids}"})
    public ResponseEntity<Object> removeById(@PathVariable("ids") @NotNull List<String> ids) {
        scriptPublishVersionService.removeByIds(ids);
        return ResponseEntity.ok("删除成功");
    }

    /**
     * @Method publish
     * @Param
     * @param scriptPublishVersion
     * @param clusterIds
     * @Return org.springframework.http.ResponseEntity<java.lang.Object>
     * @Description 发布
     * @Author Wang zhihao
     * @Date 2024/1/10 14:38
     * @Version V1.0
     */
    @ApiOperation(value = "发布", notes = "发布")
    @PostMapping(value = "/publish/{serverIds}/{clusterIds}/{publishUser}")
    public ResponseEntity<Object> publish(@RequestBody ScriptPublishVersionEntity scriptPublishVersion, @PathVariable("serverIds") String serverIds, @PathVariable("clusterIds") String clusterIds, @PathVariable("publishUser") String publishUser) {
        scriptPublishVersionService.publish(scriptPublishVersion, serverIds, clusterIds, publishUser);
        return ResponseEntity.ok("发布成功");
    }

    /**
     * @Method getUserListByCluster
     * @Param
     * @param serverId
     * @param ids
     * @Return org.springframework.http.ResponseEntity<java.lang.Object>
     * @Description 根据集群获取脚本发布可选用户列表
     * @Author Wang zhihao
     * @Date 2024/1/10 14:59
     * @Version V1.0
     */
    @ApiOperation(value = "根据集群获取脚本发布可选用户列表", notes = "根据集群获取脚本发布可选用户列表")
    //通过集群id与用户id获取脚本发布列表
    @GetMapping("/userList/{serverId}/{ids}" )
    @AnonymousAccess
    public ResponseEntity<Object> getUserListByCluster(@PathVariable("serverId" ) String serverId,@PathVariable("ids") String ids) {
        List<String> idList = Arrays.stream(ids.split(","))
                .map(String::trim) // 去除每个元素前后的空格
                .collect(Collectors.toList());
        return ResponseEntity.ok(scriptPublishVersionService.getUserListByClusterAndId(serverId,idList));
    }

}
